package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.DrmoPackage;
import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.EquipmentType;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.DrmoPackageService;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.util.Collections;
import java.util.List;

/**
 * View Helper class for DRMO Packages
 *
 * @author TSgt Jason Ferguson
 */
@Service("drmoPackageViewHelper")
public class DrmoPackageViewHelper implements PageContextAwareBean {

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    private AdpeAccountService adpeAccountService;

    @Autowired
    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    private DrmoPackageService drmoPackageService;

    @Autowired
    public void setDrmoPackageService(DrmoPackageService drmoPackageService) {
        this.drmoPackageService = drmoPackageService;
    }

    public DrmoPackage getPackage() {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int drmoPackageId = Integer.parseInt(request.getParameter("packageId"));

        return drmoPackageService.getById(drmoPackageId);
    }

    public List<DrmoPackage> getAllByAdpeAccount() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        Integer adpeAccountId = (Integer) request.getAttribute("accountId");

        // since LoginRoleAction always populates this in the session fall back to it to prevent an NPE
        if (adpeAccountId == null) {
            adpeAccountId = (Integer) request.getSession().getAttribute("accountId");
        }

        AdpeAccount acct = adpeAccountService.getById(adpeAccountId);
        List<DrmoPackage> results = drmoPackageService.getAllByAdpeAccount(acct);

        Collections.sort(results);

        return results;
    }

    public List<DrmoPackage> getPendingByAdpeAccount() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int adpeAccountId = (Integer) request.getSession().getAttribute("accountId");

        AdpeAccount acct = adpeAccountService.getById(adpeAccountId);

        return drmoPackageService.getPendingByAdpeAccount(acct);
    }

    public int getPendingCount() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int adpeAccountId = (Integer) request.getSession().getAttribute("accountId");

        AdpeAccount acct = adpeAccountService.getById(adpeAccountId);

        return drmoPackageService.getPendingCountByAdpeAccount(acct);
    }

    @SuppressWarnings({"UnnecessaryLocalVariable"})
    public EquipmentType getEquipmentType() {

        DrmoPackage pkg = getPackage();
        EquipmentType type = ((EquipmentItem[]) pkg.getIdentifiedItems().toArray())[0].getEquipmentType();
        return type;
    }
}
